package order_service

import (
	"bee/pkg/logging"
	"bee/pkg/util"
	"github.com/go-resty/resty/v2"
	"github.com/tidwall/gjson"
)

// 账户
type Account struct {
	CustomerID      string   `json:"customerId"`
	ServiceType     string   `json:"serviceType"`
	ProductIDList   []string `json:"productIdList"`
	InvoiceProperty string   `json:"invoiceProperty"`
	AccountType     string   `json:"accountType"`
	PlatformType    string   `json:"platformType"`
}

func (o Order) CreateAccount(host string) (identityId string, accountId int, err error) {

	url := host + "/api/opc/account/createAccount"

	cookie := util.SetCookie("sso-epctoken", o.Token)

	headers := make(map[string]string)
	headers["Content-Type"] = "application/json;charset=UTF-8"

	account := Account{
		CustomerID:      o.CustomerId,
		ServiceType:     "01",
		//ProductIDList:   []string{"32575", "32574", "32573"},
		ProductIDList: []string{},
		InvoiceProperty: "1",
		AccountType:     "1",
		PlatformType:    "1",
	}

	client := resty.New()
	accountResp, accountErr := client.R().EnableTrace().
		SetCookie(cookie).
		SetHeaders(headers).SetBody(account).
		Post(url)

	if accountErr != nil {
		return "",0, accountErr
	}

	identityId = gjson.Get(accountResp.String(), "data.identityId").String()
	accountId = int(gjson.Get(accountResp.String(), "data.accountId").Int())

	logging.ZapLogger.Infow("【创建客户】", "identityId", identityId, "accountId", accountId, "traceId", o.TraceId)

	return identityId, accountId, nil

}